package android.cmpe451.group7.util;

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;

/**
 * aes class.
 */
@SuppressWarnings("restriction")
public class Aes {

	/**
	 * asHex.
	 * 
	 * @param buf
	 *            an array of byte.
	 * @return a String object.
	 */
	public static String asHex(byte buf[]) {
		StringBuffer strbuf = new StringBuffer(buf.length * 2);
		int i;
		for (i = 0; i < buf.length; i++) {
			if (((int) buf[i] & 0xff) < 0x10)
				strbuf.append("0");
			strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
		}
		return strbuf.toString();
	}

	/**
	 * encryptPassword.
	 * 
	 * @param plainPassword
	 *            a String object.
	 * @return a String object.
	 * @throws java.security.NoSuchAlgorithmException
	 *             if any.
	 * @throws javax.crypto.NoSuchPaddingException
	 *             if any.
	 * @throws java.security.InvalidKeyException
	 *             if any.
	 * @throws javax.crypto.IllegalBlockSizeException
	 *             if any.
	 * @throws javax.crypto.BadPaddingException
	 *             if any.
	 */
	public static String encryptPassword(String plainPassword)
			throws NoSuchAlgorithmException, NoSuchPaddingException,
			InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		Cipher cipher;
		String pass = "_cmpe451_group7_";
		plainPassword = plainPassword + "#";
		byte[] raw = pass.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		byte[] encrypted;
		String encPass;

		cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		encrypted = cipher.doFinal(plainPassword.getBytes());
		encPass = asHex(encrypted).toUpperCase();

		return encPass;
	}

	/**
	 * decryptPassword.
	 * 
	 * @param encHexPass
	 *            a String object.
	 * @return a java.lang.String object.
	 * @throws java.security.NoSuchAlgorithmException
	 *             if any.
	 * @throws javax.crypto.NoSuchPaddingException
	 *             if any.
	 * @throws java.security.InvalidKeyException
	 *             if any.
	 * @throws javax.crypto.IllegalBlockSizeException
	 *             if any.
	 * @throws javax.crypto.BadPaddingException
	 *             if any.
	 */
	public static String decryptPassword(String encHexPass)
			throws NoSuchAlgorithmException, NoSuchPaddingException,
			InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		Cipher cipher;
		String pass = "_cmpe451_group7_";
		encHexPass = encHexPass.toLowerCase();
		byte[] raw = pass.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, skeySpec);
		byte[] decodedPassword = HexBin.decode(encHexPass);
		if (decodedPassword == null) {
			return null;
		}

		cipher.doFinal(decodedPassword);
		byte[] original = cipher.doFinal(HexBin.decode(encHexPass));
		String originalString = new String(original);
		originalString = originalString.substring(0,
				originalString.indexOf('#'));
		return originalString;
	}

	/**
	 * main.
	 * 
	 * @param args
	 *            an array of String objects.
	 * @throws java.security.InvalidKeyException
	 *             if any.
	 * @throws java.security.NoSuchAlgorithmException
	 *             if any.
	 * @throws javax.crypto.NoSuchPaddingException
	 *             if any.
	 * @throws javax.crypto.IllegalBlockSizeException
	 *             if any.
	 * @throws javax.crypto.BadPaddingException
	 *             if any.
	 */
	public static void main(String[] args) throws InvalidKeyException,
			NoSuchAlgorithmException, NoSuchPaddingException,
			IllegalBlockSizeException, BadPaddingException {

		String str = "sifre";
		String encd = "encrypt";
		String dec = "decrypt";
		String de = null;

		if (str.length() < 2) {
			// System.out.println (args.length);
			System.out
					.println("usage: aes encyrpt/decrypt cleartext_to_be_encrypted/ciphered_text_to_be_decrypted");
			return;
		}

		if (encd.equals("encrypt")) {
			String enc = encryptPassword(str);
			System.out.println(enc);
			de = enc;
		}

		if (dec.equals("decrypt")) {
			String org = decryptPassword(de);
			System.out.println(org);
		} else {
			System.out
					.println("usage: aes encyrpt/decrypt cleartext_to_be_encrypted/ciphered_text_to_be_decrypted");
		}
	}
}
